<?php
namespace Owl\Swoole\Service;

/**
 * composer update yeaha/owl
 *
 * @package Owl\Swoole\Service
 */
class SWServer extends \Owl\Service
{
    protected $client;

    public function connect()
    {
        if (!$this->client || !$this->client->isConnected()) {
            $port    = $this->getConfig('port') ?: 9651;
            $options = $this->getConfig('options') ?: [];

            try {
                $this->client = new \HC\Swoole\Client($port, $options);
            } catch (\Exception $exception) {
                \Owl\Logger::log('debug', 'swoole client connect error');
            }
        }

        return $this->client;
    }

    public function disconnect()
    {
        if ($this->client) {
            $this->client = null;
        }
    }
}